package com.yanqu.road.server.cmd.player;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

@Cmd(code = Protocol.S_SHOUQ_COUPON_REWARD, desc = "手Q卡券奖励")
public class ShouQCouponRewardCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        int channelId = ServerListMgr.getChannelId(GameServer.getInstance().getServerId());
        //手Q渠道才有
        if(eChannelType.Mobile_QQ.getValue() != channelId) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        AttributeModule attributeModule = player.getModule(AttributeModule.class);
        long rewardState = attributeModule.getAttribute(ePlayerAttrType.ShouQCouponReward);
        if (rewardState > 0) {
            ret = GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
            return;
        }
        //发邮件奖励
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(ChannelConfig.SHOU_Q_COUPON_REWARD);
        if(null == goodsInfo){
            ret = GameErrorCode.E_GOODS_NO_EXIST;
            return;
        }
        if (eGoodsType.PlatformGoods.getValue() == goodsInfo.getType() &&
                0 == goodsInfo.getParamList().get(0).compareTo(BigInteger.ONE)) {
            ret = GameErrorCode.E_PLATFORM_GOODS_IS_LOCK;
            return;
        }
        String language = player.getLanguage();
        String mailTitle = ServerLanguageMgr.getContent(goodsInfo.getMailTitle(), language);
        String mailContent = ServerLanguageMgr.getContent(goodsInfo.getMailContent(), language);
        if (StringUtils.isNullOrEmpty(mailTitle)) {
            mailTitle = MultipleLanguageMgr.getContent(MailManager.PLATFORM_MAIL_REWARD_TITLE, language);
        }
        if (StringUtils.isNullOrEmpty(mailContent)) {
            mailContent = MultipleLanguageMgr.getContent(MailManager.PLATFORM_MAIL_REWARD_CONTENT, language);
        }
        attributeModule.setAttribute(ePlayerAttrType.ShouQCouponReward, 1);
        MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parseGoodsToString(ChannelConfig.SHOU_Q_COUPON_REWARD, BigInteger.ONE),
                mailContent, mailTitle);
    }

    @Override
    public void sendData(GamePlayer player) {
        CommonProto.ResultValueMsg.Builder respMsg = CommonProto.ResultValueMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_SHOUQ_COUPON_REWARD, respMsg);
    }
}
